package com.example.myapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class CartActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private CartAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        recyclerView = findViewById(R.id.rv_cart);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 获取购物车数据
        SharedPreferences prefs = getSharedPreferences("cart_prefs", MODE_PRIVATE);
        String cartJson = prefs.getString("cart", null);
        List<Product> cartList;
        if (cartJson == null) {
            cartList = new ArrayList<>();
        } else {
            Gson gson = new Gson();
            cartList = gson.fromJson(cartJson, new com.google.gson.reflect.TypeToken<List<Product>>() {}.getType());
        }

        adapter = new CartAdapter(cartList);
        recyclerView.setAdapter(adapter);
    }

    // RecyclerView Adapter
    private class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {
        private final List<Product> cartList;

        public CartAdapter(List<Product> cartList) {
            this.cartList = cartList;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())

                    .inflate(R.layout.item_cart, parent, false);

            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Product product = cartList.get(position);
            holder.ivCartProductImage.setImageResource(product.getImageResId());
            holder.tvCartProductName.setText(product.getName());
            holder.tvCartProductPrice.setText("¥" + product.getPrice());
        }

        @Override
        public int getItemCount() {
            return cartList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            ImageView ivCartProductImage;
            TextView tvCartProductName, tvCartProductPrice;

            ViewHolder(View itemView) {
                super(itemView);
                ivCartProductImage = itemView.findViewById(R.id.iv_cart_product_image);
                tvCartProductName = itemView.findViewById(R.id.tv_cart_product_name);
                tvCartProductPrice = itemView.findViewById(R.id.tv_cart_product_price);
            }
        }
    }
}